package com.imnc.foodmap.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.imnc.foodmap.R;
import com.imnc.foodmap.model.PoiInfoParcelable;
import com.imnc.foodmap.ui.base.BaseActivity;

public class PoiResultDetailActivity extends BaseActivity {

	private ActionBar mActionBar = null;
	private PoiInfoParcelable poiInfoParcelable = null;

	private TextView mNameTextView;
	private TextView mCityTextView;
	private TextView mAddressTextView;
	private TextView mPhoneNumTextView;
	private TextView mPostCodeTextView;

	private int type = 0;

	/** 搜索 **/
	private MKSearch mSearch = null;
	private MKSearchListener mSearchListener = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_poi_result_detail);

		findView();
		initViews();
	}

	private void findView() {
		mNameTextView = (TextView) findViewById(R.id.txt_name);
		mAddressTextView = (TextView) findViewById(R.id.txt_address);
		mPhoneNumTextView = (TextView) findViewById(R.id.txt_phoneNum);
		mCityTextView = (TextView) findViewById(R.id.txt_city);
		mPostCodeTextView = (TextView) findViewById(R.id.txt_postCode);

		mSearch = new MKSearch();
		mSearchListener = new SearchListener();
		mSearch.init(mApplication.getmBMapManager(), mSearchListener);
	}

	private void initViews() {
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(true);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		poiInfoParcelable = bundle
				.getParcelable(MainActivity.MAP_ALL_POI_RESULT);

		type = bundle.getInt(MapActivity.LOAD_TYPE);

		if (poiInfoParcelable == null) {
			defaultFinish();
		}

		mAddressTextView.setText(poiInfoParcelable.address);
		mCityTextView.setText(poiInfoParcelable.city);
		mPhoneNumTextView.setText(poiInfoParcelable.phoneNum);
		mPostCodeTextView.setText(poiInfoParcelable.postCode);
		mNameTextView.setText(poiInfoParcelable.name);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getSupportMenuInflater().inflate(R.menu.poi, menu);

		MenuItem menuItem = menu.findItem(R.id.detail);
		if (!poiInfoParcelable.hasCaterDetails) {
			menuItem.setVisible(false);
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		case R.id.detail:
			if (poiInfoParcelable.hasCaterDetails) {
				mSearch.poiDetailSearch(poiInfoParcelable.uid);
			}
			break;
		case R.id.phone:
			Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
					+ poiInfoParcelable.phoneNum));
			startActivity(intent);
			break;
		case R.id.goingto:
			System.out.println("type=" + type);
			if (type == 0) {
				Intent it = new Intent(this, MapActivity.class);
				it.putExtra(MainActivity.MAP_ALL_POI_RESULT,
						poiInfoParcelable);
				it.putExtra(MapActivity.LOAD_TYPE, MapActivity.MAIN_LOAD);
				startActivity(it);
				finish();
			} else if (type == 1) {
				Intent data = new Intent();
				data.putExtra(MainActivity.MAP_ALL_POI_RESULT,
						poiInfoParcelable);
				setResult(MapActivity.REQUEST_ROUTE_PLANNING, data);
				finish();
			}
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	//
	private class SearchListener implements MKSearchListener {
		@Override
		public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult result,
				int iError) {
		}

		@Override
		public void onGetPoiDetailSearchResult(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
				int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

	}
}
